package com.aiti.lulian.controller.newCard;

import cn.hutool.core.exceptions.ExceptionUtil;
import cn.hutool.core.util.StrUtil;
import com.aiti.base.core.base.controller.WebController;
import com.aiti.base.core.exception.BusinessException;
import com.aiti.base.core.utils.message.Message;
import com.aiti.lulian.dto.newCard.*;
import com.aiti.lulian.entity.newCard.CardEnterpriseQr;
import com.aiti.lulian.entity.newCard.CardQr;
import com.aiti.lulian.service.newCard.CardEnterpriseQrService;
import com.aiti.lulian.service.newCard.CardEnterpriseUserService;
import com.aiti.lulian.service.newCard.CardQrService;
import com.aiti.lulian.service.newCard.CardShareConfigService;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.Base64;
import java.util.List;

/**
 * description:
 * auth fcb
 * date 2025-09-05 14:09
 **/
@RestController
@RequestMapping("/new-card/user/")
@Slf4j
@Api(tags = "企业名片邀请用户")
public class CardEnterpriseUserController extends WebController {

    @Autowired
    private CardEnterpriseQrService cardEnterpriseQrService;
    @Autowired
    private CardEnterpriseUserService cardEnterpriseUserService;
    @Autowired
    private CardShareConfigService cardShareConfigService;
    @Autowired
    private CardQrService cardQrService;

    @PostMapping("/generate-qr")
    @ApiOperation("生成二维码")
    public Message generateQR(@RequestBody EnterpriseCardQRParamDto paramDto) {
        try {
            String userId = paramDto.getUserId();
            if(StrUtil.isBlank(userId)) {
                userId = this.findUserId();
                if(StrUtil.isBlank(userId)) {
                    return Message.fail("请先登录");
                }
            }
            paramDto.setUserId(userId);
            return Message.success(cardEnterpriseQrService.generateQR(paramDto));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("生成二维码异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

    @PostMapping("/update-qr-expire-time")
    @ApiOperation("更新二维码过期时间")
    public Message updateQRExpireTime(@RequestBody EnterpriseCardQRParamDto paramDto) {
        try {
            String userId = this.findUserId();
            paramDto.setUserId(userId);
            return Message.success(cardEnterpriseQrService.updateQRExpireTime(paramDto));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("更新二维码过期时间异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

    @PostMapping("/generate-card-qr")
    @ApiOperation("生成名片二维码")
    public Message generateCardQR(@RequestBody EnterpriseCardQRParamDto dto) {
        try {
            String cardQRByCardId = cardQrService.getCardQRByCardId(dto);
            return Message.success(cardQRByCardId);
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("生成名片二维码异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

    @GetMapping("/download-qr")
    @ApiOperation("下载二维码")
    public void downloadQR(@RequestParam("baseId") String baseId, HttpServletResponse  response) {
        CardEnterpriseQr qrCode = cardEnterpriseQrService.getById(baseId);
        if(qrCode == null) {
            Message message = Message.fail("二维码不存在");
            this.sendJsonString(response, JSONObject.toJSONString(message));
            return;
        }
        try {
            byte[] imageData = Base64.getDecoder().decode(qrCode.getQrCode());
            response.setContentType("image/png");
            String encodedFileName = URLEncoder.encode("二维码", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-Disposition", "attachment; filename=\"" + encodedFileName + "\"; filename*=UTF-8''" + encodedFileName);
            response.setContentLength(imageData.length);

            ServletOutputStream outputStream = response.getOutputStream();
            outputStream.write(imageData);
            outputStream.flush();
            outputStream.close();
        }catch (Exception e) {
            log.error("下载二维码异常：{}", ExceptionUtil.stacktraceToString(e));
            Message message = Message.fail("操作异常");
            this.sendJsonString(response, JSONObject.toJSONString(message));
        }
    }

    @GetMapping("/download-card-qr")
    @ApiOperation("下载名片二维码")
    public void downloadCardQR(@RequestParam("cardId") String cardId, HttpServletResponse  response) {
        CardQr cardQr = cardQrService.queryByCardId(cardId);
        if(cardQr == null) {
            Message message = Message.fail("二维码不存在");
            this.sendJsonString(response, JSONObject.toJSONString(message));
            return;
        }
        try {
            byte[] imageData = Base64.getDecoder().decode(cardQr.getCardCode());
            response.setContentType("image/png");
            String encodedFileName = URLEncoder.encode("二维码", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-Disposition", "attachment; filename=\"" + encodedFileName + "\"; filename*=UTF-8''" + encodedFileName);
            response.setContentLength(imageData.length);

            ServletOutputStream outputStream = response.getOutputStream();
            outputStream.write(imageData);
            outputStream.flush();
            outputStream.close();
        }catch (Exception e) {
            log.error("下载二维码异常：{}", ExceptionUtil.stacktraceToString(e));
            Message message = Message.fail("操作异常");
            this.sendJsonString(response, JSONObject.toJSONString(message));
        }
    }

    //-----------------------邀请员工页面--------------------------------
    @GetMapping("/get-enterprise-admin-invite-user-info")
    @ApiOperation("获取企业管理员邀请用户信息")
    public Message getEnterpriseAdminInviteUserInfo(@RequestParam("enterpriseId") String enterpriseId,
                                                    @RequestParam("templateId") String templateId,
                                                    @RequestParam("userId") String userId,
                                                    @RequestParam(value = "type", required = false, defaultValue = "1")String type,
                                                    @RequestParam(value = "auditBaseId", required = false)String auditBaseId) {
        try {
            return Message.success(cardEnterpriseUserService.getEnterpriseAdminInviteUserInfo(enterpriseId, templateId, userId, type, auditBaseId));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("获取企业管理员邀请用户信息异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("查询异常");
        }
    }

    @GetMapping("/get-style-field-list")
    @ApiOperation("获取样式字段列表")
    public Message getStyleFieldList(@RequestParam("templateId") String templateId) {
        try {
            return Message.success(cardEnterpriseUserService.getStyleFieldList(templateId));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("获取样式字段列表异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("查询异常");
        }
    }

    @PostMapping("/create-enterprise-user-card")
    @ApiOperation("创建企业用户名片--待审核")
    public Message createEnterpriseUserCard(@RequestBody CreateEnterpriseUserCardDto dto) {
        try {
            return cardEnterpriseUserService.createEnterpriseUserCard(dto);
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("创建企业用户名片异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

    @PutMapping("/get-my-card")
    @ApiOperation("小程序端领取我的名片")
    public Message getMyCard( @RequestParam("userId") String userId,
                              @RequestParam("auditBaseId") String auditBaseId) {
        try {
            return cardEnterpriseUserService.getMyCard(userId, auditBaseId);
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("小程序端领取我的名片异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

    @PostMapping("/create-enterprise-user-card2")
    @ApiOperation("创建企业用户名片--待激活")
    public Message createEnterpriseUserCard2(@RequestBody CreateEnterpriseUserCardDto dto) {
        try {
            String userId = this.findUserId();
            if(StrUtil.isBlank(userId)) {
                if(StrUtil.isBlank(dto.getUserId())) {
                    return Message.fail("请先登录");
                }
                userId = dto.getUserId();
            }
            cardEnterpriseUserService.createEnterpriseUserCard2(dto, userId);
            return Message.success();
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("创建企业用户名片异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

    @PostMapping("/query-enterprise-user-card-list")
    @ApiOperation("查询企业用户名片列表")
    public Message queryEnterpriseUserCardList(@RequestBody EnterpriseUserCardSearchDto queryDto) {
        try {
            return Message.success(cardEnterpriseUserService.queryEnterpriseUserCardList(queryDto));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询企业用户名片列表异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("查询异常");
        }
    }

    @GetMapping("/card-style-and-data-detail")
    @ApiOperation("获取名片样式和数据详情")
    public Message cardStyleAndDataDetail(@RequestParam("templateId") String templateId,
                                          @RequestParam("cardId") String cardId) {
        try {
            return Message.success(cardEnterpriseUserService.cardStyleAndDataDetail(templateId, cardId));
        } catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("获取名片样式和数据详情异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("查询异常");
        }
    }

    @GetMapping("/get-enterprise-user-all-card-style")
    @ApiOperation("获取某企业下用户所有名片样式和字段数据")
    public Message getUserAllCardStyleAndFieldData(@RequestParam("enterpriseId") String enterpriseId,
                                                   @RequestParam("userId") String userId) {
        try {
            return Message.success(cardEnterpriseUserService.getUserAllCardStyleAndFieldData(enterpriseId, userId));
        } catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("获取用户所有名片样式和字段数据异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("查询异常");
        }
    }

    @PostMapping("/update-card-field-value")
    @ApiOperation("更新名片字段值")
    public Message updateCardFieldValue(@RequestBody CreateEnterpriseUserCardDto dto) {
        try {
            String userId = dto.getUserId();
            if(StrUtil.isBlank(userId)) {
                userId = findUserId();
                if(StrUtil.isBlank(userId)) {
                    return Message.fail("请先登录");
                }
            }
            dto.setUserId(userId);
            cardEnterpriseUserService.updateCardFieldValue(dto);
            return Message.success();
        } catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("更新名片字段值异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

    @PostMapping("/update-card-audit-status")
    @ApiOperation("更新名片审核状态")
    public Message updateCardAuditStatus(@RequestBody UpdateCardStatusDto dto) {
        try {
            if(StrUtil.isBlank(dto.getUserId())) {
                dto.setUserId(findUserId());
                if(StrUtil.isBlank(dto.getUserId())) {
                    return Message.fail("请先登录");
                }
            }
            cardEnterpriseUserService.updateCardAuditStatus(dto);
            return Message.success();
        } catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("更新名片审核状态异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

    @GetMapping("/change-template")
    @ApiOperation("更换模板")
    public Message changeTemplate(@RequestParam("sourceTemplateId") String sourceTemplateId,
                                  @RequestParam("targetTemplateId") String targetTemplateId,
                                  @RequestParam("cardId") String cardId) {
        try {
            return Message.success(cardEnterpriseUserService.changeTemplate(sourceTemplateId, targetTemplateId, cardId));
        } catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("更换模板异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

    @PostMapping("/batch-change-template")
    @ApiOperation("批量更换模板")
    public Message batchChangeTemplate(@RequestBody BatchChangeTemplateDto dto) {
        try {
            String userId = this.findUserId();
            if(StrUtil.isBlank(userId)) {
                return Message.fail("请先登录");
            }
            dto.setUserId(userId);
            cardEnterpriseUserService.batchChangeTemplate(dto);
            return Message.success();
        } catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("更换模板异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

    @GetMapping("/get-can-edit-field-list")
    @ApiOperation("获取可编辑字段列表")
    public Message getCanEditFieldList(@RequestParam("templateId") String templateId) {
        try {
            return Message.success(cardEnterpriseUserService.getBatchEditStyleFieldList(templateId));
        } catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("获取可编辑字段列表异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

    @PostMapping("/batch-update-card-field-value")
    @ApiOperation("批量更新名片字段值")
    public Message batchUpdateCardFieldValue(@RequestBody StyleFieldDto dto) {
        try {
            cardEnterpriseUserService.batchUpdateCardFieldValue(dto);
            return Message.success();
        } catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("批量更新名片字段值异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

    @PutMapping("/update-template-name")
    @ApiOperation("更新模板名称")
    public Message updateTemplateName(@RequestParam("templateId") String templateId,
                                      @RequestParam("templateName") String templateName) {
        try {
            String userId = findUserId();
            if(StrUtil.isBlank(userId)) {
                return Message.fail("请先登录");
            }
            cardEnterpriseUserService.updateTemplateName(templateId, templateName, userId);
            return Message.success();
        } catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("更新模板名称异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

    @GetMapping("/get-template-style-and-field")
    @ApiOperation("获取模板样式和字段")
    public Message getTemplateStyleAndField(@RequestParam("templateId") String templateId) {
        try {
            return Message.success(cardEnterpriseUserService.getTemplateStyleAndField(templateId));
        } catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("获取模板样式和字段异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

    @GetMapping("/get-template-card-count")
    @ApiOperation("获取模板名片数量")
    public Message getTemplateCardCount(@RequestParam("enterpriseId") String enterpriseId,
                                       @RequestParam(value = "templateId", required = false) String templateId) {
        try {
            return Message.success(cardEnterpriseUserService.getTemplateCardCount(enterpriseId, templateId));
        } catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("获取模板名片数量异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

    @GetMapping("/download-import-excel-template")
    @ApiOperation("下载导入Excel模板")
    public void downloadImportExcelTemplate(@RequestParam("enterpriseId") String enterpriseId,
                                            @RequestParam("templateId") String templateId,
                                            HttpServletResponse response) {
        cardEnterpriseUserService.downloadImportExcelTemplate(enterpriseId, templateId, response);
    }

    @GetMapping("/export-template-card-data")
    @ApiOperation("导出模板名片数据")
    public void exportTemplateCardData(@RequestParam("templateId") String templateId,
                                       @RequestParam("status") Integer status,
                                       HttpServletResponse response) {
        cardEnterpriseUserService.exportTemplateCardData(templateId, status, response);
    }

    @PostMapping("/import-excel-data")
    @ApiOperation("导入Excel数据")
    public Message importExcelData(@RequestParam("enterpriseId")String enterpriseId,
                                   @RequestParam("templateId") String templateId,
                                   @RequestParam("file") MultipartFile file) {
        try {
            String userId = this.findUserId();
            if(StrUtil.isBlank(userId)) {
                return Message.fail("请先登录");
            }
            cardEnterpriseUserService.importExcelData(enterpriseId, templateId, userId, file);
            return Message.success();
        } catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("导入Excel数据异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

    // ---------------------------------转发配置----------------------------------
    @GetMapping("/query-default-cover-url")
    @ApiOperation("查询默认封面")
    public Message queryDefaultCoverUrl(@RequestParam(value = "templateId") String templateId) {
        try {
            return Message.success(cardShareConfigService.queryDefaultCoverUrl(templateId));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询默认封面异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

    @GetMapping("/query-template-share-config")
    @ApiOperation("查询模板分享配置")
    public Message queryTemplateShareConfig(@RequestParam("enterpriseId") String enterpriseId,
                                            @RequestParam("templateId") String templateId) {
        try {
            return Message.success(cardShareConfigService.queryCardShareConfig(enterpriseId, templateId));
        } catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询模板分享配置异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

    @PostMapping("/update-card-share-config")
    @ApiOperation("更新模板分享配置")
    public Message updateCardShareConfig(@RequestBody CardShareConfig cardShareConfig) {
        try {
            String userId = findUserId();
            if(StrUtil.isBlank(userId)) {
                return Message.fail("用户未登录");
            }
            cardShareConfigService.updateCardShareConfig(cardShareConfig, userId);
            return Message.success();
        } catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("更新模板分享配置异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

    @GetMapping("/query-card-forward-config")
    @ApiOperation("小程序--查询名片的转发配置")
    public Message queryTemplateShareConfig(@RequestParam("cardId")String cardId) {
        try {
            return Message.success(cardShareConfigService.queryCardForwardInfo(cardId));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询模板分享配置异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

    @PostMapping("/update-card-forward-info")
    @ApiOperation("小程序--修改名片转发配置")
    public Message updateCardForwardInfo(@RequestBody UpdateCardForwardDto updateCardForwardDto) {
        try {
            cardShareConfigService.updateCardForwardInfo(updateCardForwardDto);
            return Message.success();
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("更新模板分享配置异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

    @PostMapping("/update-card-forward-coverUrl")
    @ApiOperation("小程序--修改名片转发配置")
    public Message updateCardForwardCoverUrl(@RequestBody UpdateCardForwardDto updateCardForwardDto) {
        try {
            cardShareConfigService.updateCardForwardUrlByCardId(updateCardForwardDto);
            return Message.success();
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("更新名片转发封面异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

    @GetMapping("/get-enterprise-user-active")
    @ApiOperation("小程序--查询加入企业状态")
    public Message getEnterpriseUserActive(@RequestParam("pageNum") Integer pageNum,
                                           @RequestParam("pageSize") Integer pageSize,
                                           @RequestParam("enterpriseId")String enterpriseId) {
        try {
            return Message.success(cardEnterpriseUserService.queryEnterpriseUserActive(pageNum, pageSize, enterpriseId));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询加入企业状态：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

    @GetMapping("/get-enterprise-apply-msg")
    @ApiOperation("小程序--查询申请加入企业信息")
    public Message getEnterpriseApplyMsg(@RequestParam("pageNum") Integer pageNum,
                                         @RequestParam("pageSize") Integer pageSize,
                                         @RequestParam("enterpriseId")String enterpriseId) {
        try {
            return Message.success(cardEnterpriseUserService.queryEnterpriseApplyMsg(pageNum, pageSize, enterpriseId));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询申请加入企业信息：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

    @GetMapping("/get-enterprise-card-use-info")
    @ApiOperation("小程序--查询企业名片使用信息")
    public Message getEnterpriseCardUseInfo(@RequestParam("enterpriseId")String enterpriseId) {
        try {
            return Message.success(cardEnterpriseUserService.queryEnterpriseCardUseInfo(enterpriseId));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询企业名片使用信息：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

    @PostMapping("/get-enterprise-user-card-use-info")
    @ApiOperation("小程序--查询用户使用信息")
    public Message getEnterpriseUserCardUseInfo(@RequestBody EnterpriseUserCardUseInfoDto dto) {
        try {
            return Message.success(cardEnterpriseUserService.queryEnterpriseUserCardUseInfo(dto));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询用户使用信息：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

    @GetMapping("/get-enterprise-card-detail")
    @ApiOperation("小程序--查询名片详情")
    public Message getEnterpriseCardDetail(@RequestParam("cardId") String cardId) {
        try {
            return Message.success(cardEnterpriseUserService.getEnterpriseCardDetail(cardId));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询名片详情异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

    @GetMapping("/enterprise-admin")
    @ApiOperation("小程序--查询是否是管理员")
    public Message isEnterpriseAdmin(@RequestParam("enterpriseId") String enterpriseId,
                                     @RequestParam("userId") String userId) {
        try {
            return Message.success(cardEnterpriseUserService.isEnterpriseAdmin(enterpriseId, userId));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询是否是管理员异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

    @GetMapping("/get-enterprise-overdue")
    @ApiOperation("查询企业是否过期")
    public Message getEnterpriseOverdue(@RequestParam("enterpriseId") String enterpriseId) {
        try {
            return Message.success(cardEnterpriseUserService.getEnterpriseOverdue(enterpriseId));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询是否过期异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

    @GetMapping("/enterprise-template-user-exist")
    @ApiOperation("查询用户是否已存在该模板")
    public Message userExistTemplate(@RequestParam("enterpriseId") String enterpriseId,
                                     @RequestParam("templateId") String templateId,
                                     @RequestParam("userId") String userId) {
        try {
            return Message.success(cardEnterpriseUserService.userExistTemplate(enterpriseId, templateId, userId));
        }catch (Exception e) {
            if(e instanceof BusinessException) {
                return Message.fail(e.getMessage());
            }
            log.error("查询用户是否已存在该模板异常：{}", ExceptionUtil.stacktraceToString(e));
            return Message.fail("操作异常");
        }
    }

    private void sendJsonString(HttpServletResponse response, String jsonString) {
        try {
            response.setContentType("application/json;charset=utf-8");
            response.getWriter().write(jsonString);
        } catch (Exception e) {
            log.error("发送json字符串异常：{}", ExceptionUtil.stacktraceToString(e));
        }
    }

}
